﻿using System;
using System.Web.UI.WebControls;
using MONTRASEC_test.Controls;
using MONTRASEC_test.Classes;
namespace MONTRASEC_test.GroundLevel
{
  public partial class B_3_Authors : System.Web.UI.Page
  {
    protected void Page_Load_from_template(object sender, EventArgs e)
    {
      //don't change this
      if (!IsPostBack)
      {
        Session["CurrentANewEntryID"] = null;
      }
      myCurrentTable = myDVB3Authors;
      Subscribe(myGVANewEntry);
      Subscribe(mytemplateHelperTopButtons);
      Subscribe(myDVB3Authors);
    }
    // given a Gridview, subscribe to its SelectedIndexChangedHandler event
    public void Subscribe(wucDGANewEntry theDGANewEntry)
    {
      // don't change this!
      theDGANewEntry.SelectedIndexChanged += new wucDGANewEntry.SelectedIndexChangedHandler(gvSelectedIndexHasChanged);

    }
    public void gvSelectedIndexHasChanged(object sender, EventArgs e)
    {
      //mytemplateHelperTopButtons.createNewRecordButton.Visible = true;
      //mytemplateHelperTopButtons.refreshPageButton.Visible = true;
      myCurrentTable.dv.DataBind();

      if (myCurrentTable.dv.DataItemCount == 0)
      {
        StatusDVMode = "You can 'Create a new record' for the selected Entry";
      }
      else
      {
        StatusDVMode = UtilHelper.getTextForDVMode(myCurrentTable.dv.CurrentMode);
      }
      myCurrentTable.dv.ChangeMode(DetailsViewMode.ReadOnly);

      UpdatePageBasedOnStatusOfControls();
    }

    //put here all controls that must update their content (eg) when a button has been clicked
    private void UpdatePageBasedOnStatusOfControls()
    {
      myCurrentTable.LDVMode.Text = StatusDVMode;
      myCurrentTable.LbTotalRecords.Text = " - New Entry ID: " + UtilHelper.getSelectedRecord(Session["CurrentANewEntryID"]);
      UtilHelper.getInvitationToFillTemplates(myCurrentTable.BPleaseFillOtherTemplates, Session["CurrentANewEntryID"].ToString());
      //myCurrentTable.LbTotalRecords.Text = myCurrentTable.dv.DataItemCount.ToString();

    }


    public void buCreateNewRecordIsPushed(object sender, EventArgs e)
    {
      myCurrentTable.dv.ChangeMode(DetailsViewMode.Insert);
      StatusDVMode = UtilHelper.getTextForDVMode(myCurrentTable.dv.CurrentMode);
      UpdatePageBasedOnStatusOfControls();
    }


    // given a TemplateHelperTopButtons, subscribe to its buCreateNewRecord_Clicked event
    public void Subscribe(wucTemplateHelperTopButtonsGL theTemplateHelperTopButtons)
    {
      // don't change this!
      theTemplateHelperTopButtons.CreateNewRecord_Clicked += new wucTemplateHelperTopButtonsGL.buCreateNewRecord_ClickHandler(buCreateNewRecordIsPushed);
      theTemplateHelperTopButtons.ExportExcel_Clicked += new wucTemplateHelperTopButtonsGL.buExportExcel_ClickHandler(buExportExcelIsPushed);
    }

    public void buExportExcelIsPushed(object sender, EventArgs e)
    {
      // link to the current DetailsView
      //this.gvANewEntry.AllowPaging = false;

      //Bind the data
      //this.gvANewEntry.DataBind();
      //GridViewExportUtil.Export("A_NEWENTRY.xls", myCurrentTable.dv); // null becomes this.gvANewEntry
    }

    // given a DetailedView, subscribe to its dvANewEntry_ModeChanged event
    public void Subscribe(wucDVB3Authors theDetailsView)
    {
      // don't change this!
      theDetailsView.DetailsView_ModeChanged += new wucDVB3Authors.detailsView_ModeChangedHandler(detailsView_ModeChanged);
      theDetailsView.DetailsView_ItemInserted += new wucDVB3Authors.detailsView_ItemInsertedHandler(detailsView_ItemInserted);
      theDetailsView.DetailsView_ItemUpdated += new wucDVB3Authors.detailsView_ItemUpdatedHandler(detailsView_ItemUpdated);
      theDetailsView.DetailsView_ItemDeleted += new wucDVB3Authors.detailsView_ItemDeletedHandler(detailsView_ItemDeleted);
    }
    public void detailsView_ModeChanged(object sender, EventArgs e)
    {
      StatusDVMode = UtilHelper.getTextForDVMode(myCurrentTable.dv.CurrentMode);
      UpdatePageBasedOnStatusOfControls();
    }
    public void detailsView_ItemDeleted(object sender, EventArgs e)
    {
      StatusDVMode = "Record has been deleted";

      UpdatePageBasedOnStatusOfControls();
      Response.Redirect(Request.Url.LocalPath);
    }
    public void detailsView_ItemUpdated(object sender, EventArgs e)
    {
      StatusDVMode = "Record has been Updated";

      UpdatePageBasedOnStatusOfControls();
      Response.Redirect(Request.Url.LocalPath);
    }
    public void detailsView_ItemInserted(object sender, EventArgs e)
    {
      StatusDVMode = "Record has been inserted";

      UpdatePageBasedOnStatusOfControls();
      Response.Redirect(Request.Url.LocalPath);
    }

    String StatusDVMode = "";
    internal static wucDVB3Authors myCurrentTable;
  }
}
